{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "           特征1        特征2        特征3       特征4        特征5       特征6  类别\n0     8.877052   0.613192   8.863304  2.946280   5.335545 -7.649963   0\n1     8.638193 -11.521731  -4.600399 -1.557374   4.141176 -4.489721   1\n2     6.827690   9.776964  -7.193543  1.761037 -10.667587 -1.348013   2\n3     6.574706  12.689264  -6.477097  1.563207 -10.730840 -2.893230   2\n4     6.473263   9.381854  -6.962404  2.283727  -9.056942 -1.157163   2\n..         ...        ...        ...       ...        ...       ...  ..\n282   8.342987 -11.697379  -5.565665 -1.816157   6.090164 -6.606016   1\n283   6.935426   9.010926  -7.781119  2.954493 -10.720857 -1.641559   2\n284   8.439816   1.506481  10.840713  3.209384   3.902827 -6.444864   0\n285   8.195368  10.117439  -7.318015  2.534670  -9.727004 -2.855708   2\n286  10.531893  -9.845236  -4.303578  0.008442   6.655211 -6.092250   1\n\n[287 rows x 7 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>特征1</th>\n      <th>特征2</th>\n      <th>特征3</th>\n      <th>特征4</th>\n      <th>特征5</th>\n      <th>特征6</th>\n      <th>类别</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>8.877052</td>\n      <td>0.613192</td>\n      <td>8.863304</td>\n      <td>2.946280</td>\n      <td>5.335545</td>\n      <td>-7.649963</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>8.638193</td>\n      <td>-11.521731</td>\n      <td>-4.600399</td>\n      <td>-1.557374</td>\n      <td>4.141176</td>\n      <td>-4.489721</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>6.827690</td>\n      <td>9.776964</td>\n      <td>-7.193543</td>\n      <td>1.761037</td>\n      <td>-10.667587</td>\n      <td>-1.348013</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>6.574706</td>\n      <td>12.689264</td>\n      <td>-6.477097</td>\n      <td>1.563207</td>\n      <td>-10.730840</td>\n      <td>-2.893230</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>6.473263</td>\n      <td>9.381854</td>\n      <td>-6.962404</td>\n      <td>2.283727</td>\n      <td>-9.056942</td>\n      <td>-1.157163</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>282</th>\n      <td>8.342987</td>\n      <td>-11.697379</td>\n      <td>-5.565665</td>\n      <td>-1.816157</td>\n      <td>6.090164</td>\n      <td>-6.606016</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>283</th>\n      <td>6.935426</td>\n      <td>9.010926</td>\n      <td>-7.781119</td>\n      <td>2.954493</td>\n      <td>-10.720857</td>\n      <td>-1.641559</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>284</th>\n      <td>8.439816</td>\n      <td>1.506481</td>\n      <td>10.840713</td>\n      <td>3.209384</td>\n      <td>3.902827</td>\n      <td>-6.444864</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>285</th>\n      <td>8.195368</td>\n      <td>10.117439</td>\n      <td>-7.318015</td>\n      <td>2.534670</td>\n      <td>-9.727004</td>\n      <td>-2.855708</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>286</th>\n      <td>10.531893</td>\n      <td>-9.845236</td>\n      <td>-4.303578</td>\n      <td>0.008442</td>\n      <td>6.655211</td>\n      <td>-6.092250</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n<p>287 rows × 7 columns</p>\n</div>"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iris = pd.read_excel('分类训练数据.xlsx', sheet_name='Sheet1')\n",
    "iris"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "# 有监督的学习(特征和标签进行分类处理)\n",
    "X = iris[['特征1', '特征2', '特征3', '特征4', '特征5', '特征6']]\n",
    "Y = iris['类别']\n",
    "# 将获取到的数据分割成功一些是用来进行测试的数据 一些是用来用来训练的数据集 3:7的比例进行分配\n",
    "X_train, X_test, Y_train, Y_test = train_test_split(\n",
    "    X, Y, test_size=0.3\n",
    ")  # 第一个参数：特征   第二个参数：标签   test_size：训练集与测试集的比值"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "KNeighborsClassifier()"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier  # K近邻\n",
    "\n",
    "knn = KNeighborsClassifier()\n",
    "knn.fit(X_train, Y_train)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [],
   "source": [
    "data = pd.DataFrame()\n",
    "data2 = pd.read_excel('分类训练数据.xlsx', sheet_name='Sheet2')\n",
    "# 创建一个测试数据\n",
    "test_data = data2[['特征1', '特征2', '特征3', '特征4', '特征5', '特征6']]\n",
    "data2['类别'] = knn.predict(test_data)\n",
    "data2\n",
    "data2.to_excel('预测的数据是.xls', index=False, sheet_name='sheet1',)\n",
    "# data['真实值'] = Y_test\n",
    "# data['预测值'] = knn.predict(X_test)\n",
    "# data"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}